package com.mz.ozy.activity.find;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mz.ozy.R;
import com.mz.ozy.activity.MainActivity;
import com.mz.ozy.adapter.StrategyAdapter;
import com.mz.ozy.base.BaseFragment;
import com.mz.ozy.bean.ArticleBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.StrategyPresenter;
import com.mz.ozy.mvp.view.StrategyContract;
import com.mz.ozy.widget.dialog.NotVipDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2019/3/26.
 */

public class StrategyFragmet extends BaseFragment<StrategyContract.View, StrategyPresenter> implements StrategyContract.View {
    private static final String ARG_SHOW_TEXT = "text";
    private static final String INDEX = "index";

    private int tag;
    private int page;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.rv_course_comment)
    RecyclerView mRvCourseComment;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;

    private StrategyAdapter strategyAdapter;
    private String mType;
    private List<ArticleBean> mBeanList = new ArrayList<>();
    private String mSearchkey;

    public StrategyFragmet() {
    }

    public static StrategyFragmet newInstance(int index, String param1) {
        StrategyFragmet fragment = new StrategyFragmet();
        Bundle args = new Bundle();
        args.putString(ARG_SHOW_TEXT, param1);
        args.putInt(INDEX, index);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_find_strategy, container, false);
        ButterKnife.bind(this, rootView);
        mType = getArguments().getString(ARG_SHOW_TEXT);

        initRefreshLayout();
        initView();
//        mRefreshLayout.autoRefresh();
        requestDataForNetwork();
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
//        requestDataForNetwork();
    }

    private void initRefreshLayout() {
        mRefreshLayout.setEnableLoadmore(true);
        mRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                tag = 1;
                page = page + 1;
                mPresenter.queryArticleListByType(getActivity(), mType, mSearchkey, page, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                requestDataForNetwork();
            }
        });
    }

    private void requestDataForNetwork() {
        tag = 0;
        page = 1;
        mPresenter.queryArticleListByType(getActivity(), mType, mSearchkey, page, false);
    }

    private void initView() {
        mRvCourseComment.setLayoutManager(new LinearLayoutManager(getActivity()));
//        strategyAdapter = new StrategyAdapter(getTestCourseList());
        strategyAdapter = new StrategyAdapter(mBeanList);
        mRvCourseComment.setAdapter(strategyAdapter);
        strategyAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                ArticleBean item = strategyAdapter.getItem(position);
                switch (view.getId()) {
                    case R.id.item_root_view: {
//                        if (!Constants.isVip) {
//                            NotVipDialog dialog = new NotVipDialog(getActivity());
//                            dialog.showDialog();
//                            return;
//                        }
                        Intent intent = new Intent(getActivity(), StrategyDetailActivity.class);
                        intent.putExtra(StrategyDetailActivity.ARTICLE_ID, item.getId());
                        startActivity(intent);
                        break;
                    }
                    case R.id.ll_strategy_likenum: {
                        mPresenter.followTheObject(getActivity(), item.getId(), 1);
                        break;
                    }
                }
            }
        });
    }

    @Override
    public void getArticleListByType(JSONObject result) {
        JSONObject page = result.optJSONObject("p");
        List<ArticleBean> lists = new Gson().fromJson(page.optString("pageItems"), new TypeToken<ArrayList<ArticleBean>>() {
        }.getType());
        if (tag == 0) {
            mRefreshLayout.finishRefresh();
            mBeanList.clear();
            if (lists.size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }
        } else if (tag == 1) {
            mRefreshLayout.finishLoadmore();
        }
        mBeanList.addAll(lists);
        strategyAdapter.notifyDataSetChanged();
        mRefreshLayout.setLoadmoreFinished(page.optInt("pageSize") * page.optInt("pageNumber") >= page.optInt("pageTotle"));
    }

    @Override
    public void followSuccess(JSONObject listBean) {
        requestDataForNetwork();
    }

    public void searchArticle(String searchkey, boolean load) {
        mSearchkey = searchkey;
        tag = 0;
        page = 1;
        mPresenter.queryArticleListByType(getActivity(), mType, searchkey, page, load);
    }
}
